package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Role;
import com.itheima.service.RoleService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @descriptions: ...
 * @author: 唐杰
 * @date: 2019/11/20  19:42
 * @version: 1.0
 */
@RestController
@RequestMapping("/role")
public class RoleController {
    @Reference
    private RoleService roleService;

    /*
    * 根据条件分页查询
    * */
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        return roleService.findPage(queryPageBean);
    }

    /*
     * 查询所有角色信息
     * */
    @RequestMapping("/findAll")
    public Result findAll(){
        try {
            List<Role> roleList = roleService.findAll();
            //查询成功
            return new Result(true,"查询所有角色信息成功",roleList);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"查询所有角色信息失败");
        }
    }

    /*
    *根据id查询角色信息
    * */
    @RequestMapping("/findById")
    public Result findById(Integer id){
        try {
            Role role = roleService.findById(id);
            //查询成功
            return new Result(true,"查询角色信息成功",role);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"查询角色信息失败");
        }
    }
    /*
    * 新增角色
    * */
    @RequestMapping("/add")
    public Result add(@RequestBody Role role,Integer[] permissionIds,Integer[] menuIds){
        try {
            roleService.add(role,permissionIds,menuIds);
            //添加成功
            return new Result(true,"添加角色信息成功");
        } catch (Exception e) {
            e.printStackTrace();
            //添加失败
            return new Result(false,"添加角色信息失败");
        }
    }

    /*
    * 编辑角色
    * */
    @RequestMapping("/edit")
    public Result edit(@RequestBody Role role,Integer[] permissionIds,Integer[] menuIds){
        try {
            roleService.edit(role,permissionIds,menuIds);
            //编辑成功
            return new Result(true,"编辑角色信息成功");
        } catch (Exception e) {
            e.printStackTrace();
            //编辑失败
            return new Result(false,"编辑角色信息失败");
        }
    }

    /*
    * 删除角色数据
    * */
    @RequestMapping("/delete")
    public Result delete(Integer id){
        try {
            roleService.delete(id);
            //删除成功
            return new Result(true,"删除角色信息成功");
        } catch (Exception e) {
            e.printStackTrace();
            //删除失败
            return new Result(false,"删除角色信息失败");
        }
    }

    /*
    * 回显权限复选框数据
    * */
    @RequestMapping("/findPermissionIdsByRoleId")
    public Result findPermissionIdsByRoleId(Integer id){
        try {
            List<Integer> permissionIds =  roleService.findPermissionIdsByRoleId(id);
            //查询成功
            return new Result(true,"查询权限复选框数据成功",permissionIds);
        } catch (Exception e) {
            e.printStackTrace();
            //查询失败
            return new Result(false,"查询权限复选框数据失败");
        }
    }

    /*
    * 回显菜单复选框数据
    * */
    @RequestMapping("/findMenuIdsByRoleId")
    public Result findMenuIdsByRoleId(Integer id){
        try {
            List<Integer> menuIds = roleService.findMenuIdsByRoleId(id);
            //查询成功
            return new Result(true,"查询菜单复选框数据成功",menuIds);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"查询菜单复选框数据失败");
        }
    }
}
